package com.gy.edu.teach;

import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gy.edu.R;
import com.gy.edu.base.BaseFragment;
import com.gy.edu.teach.adapter.TeachHomeAdapter;
import com.gy.edu.util.AnimUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 16/12/21
 * 说明：微教材
 */
public class TeachHomeFragment extends BaseFragment {

    private ListView listView;
    private RelativeLayout rlNoamel;
    private LinearLayout llSelect;
    private TextView tvSelect, tvDelete;
    private TeachHomeAdapter adapter;
    private List<Integer> selects;//选中的条目
    private int isEdit;//是否编辑状态 1是
    private int isSelectAll;//是否为全选状态 1是

    @Override
    protected View setView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_teach_home, null);
        _baseBack.setVisibility(View.INVISIBLE);
        setTitleMsg("微教材");
        _baseRight_text.setText("取消");
        _baseRight_text.setVisibility(View.GONE);
        _imgRight.setImageResource(R.mipmap.download_del_icon);
        return view;
    }

    @Override
    protected void initView() {
        listView = (ListView) _rootView.findViewById(R.id.teach_home_list);
        rlNoamel = (RelativeLayout) _rootView.findViewById(R.id.teach_home_ll_normal);
        llSelect = (LinearLayout) _rootView.findViewById(R.id.teach_home_ll_select);
        tvSelect = (TextView) _rootView.findViewById(R.id.teach_home_tv_select);
        tvDelete = (TextView) _rootView.findViewById(R.id.teach_home_tv_delete);
    }

    @Override
    protected void initData() {

        selects = new ArrayList<>();
        adapter = new TeachHomeAdapter(mContext);
    }

    @Override
    protected void setListener() {
        super.setListener();
        tvSelect.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (isEdit == 1) {
                    if (selects.contains(i)) {
                        selects.remove(Integer.valueOf(i));
                    } else {
                        selects.add(Integer.valueOf(i));
                    }
                    adapter.setSelects(selects);
                    if(selects.size() > 0){
                        tvDelete.setText("删除("+selects.size()+")");
                        tvDelete.setTextColor(Color.parseColor("#ff0000"));
                    } else {
                        tvDelete.setText("删除");
                        tvDelete.setTextColor(Color.parseColor("#999999"));
                    }
                } else {
                    Intent intent = new Intent(mContext,TeachDetailActivity.class);
                    startActivity(intent);
                    AnimUtil.pushLeftInAndOut(getActivity());
                }
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id._right:
                if (isEdit == 0) {
                    isEdit = 1;
                    _baseRight_text.setVisibility(View.VISIBLE);
                    _imgRight.setVisibility(View.GONE);
                    llSelect.setVisibility(View.VISIBLE);
                    rlNoamel.setVisibility(View.GONE);
                    tvDelete.setText("删除");
                    tvDelete.setTextColor(Color.parseColor("#999999"));
                } else {
                    isEdit = 0;
                    _baseRight_text.setVisibility(View.GONE);
                    _imgRight.setVisibility(View.VISIBLE);
                    llSelect.setVisibility(View.GONE);
                    rlNoamel.setVisibility(View.VISIBLE);
                    tvDelete.setText("删除");
                    tvDelete.setTextColor(Color.parseColor("#999999"));
                }
                adapter.setIsEdit(isEdit);
                break;
            case R.id.teach_home_tv_select:
                if (isSelectAll == 0) {
                    //全选
                    isSelectAll = 1;
                    for (int i = 0; i < 3; i++) {
                        selects.add(i);
                    }
                    adapter.setSelects(selects);
                    tvSelect.setText("取消全选");
                    tvDelete.setText("删除(3)");
                    tvDelete.setTextColor(Color.parseColor("#ff0000"));
                } else if (isSelectAll == 1) {
                    //取消全选
                    isSelectAll = 0;
                    selects.clear();
                    adapter.notifyDataSetChanged();
                    tvSelect.setText("全选");
                    tvDelete.setText("删除");
                    tvDelete.setTextColor(Color.parseColor("#999999"));
                }
                break;
            case R.id.teach_home_tv_delete:

                break;
        }
    }
}
